package com.wondersgroup.esclient.service.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wondersgroup.esclient.feign.FeignUitls;
import com.wondersgroup.esclient.modle.ESData;
import com.wondersgroup.esclient.modle.Pager;
import com.wondersgroup.esclient.modle.QueryParam;
import com.wondersgroup.esclient.service.BaseESService;
import com.wondersgroup.esclient.service.ESClientService;
import com.wondersgroup.esclient.util.ESDataUtil;

public class ESClientServiceImpl<T> extends BaseESService implements ESClientService<T> {
	
	private static Logger LOGGER = LogManager.getLogger(ESClientServiceImpl.class);

	private Class<T> clazz;

	public ESClientServiceImpl() {
		super();
	}

	public ESClientServiceImpl(Class<T> clazz) {
		super();
		this.clazz = clazz;
	}

	public ESClientServiceImpl(Class<T> clazz, String clusterNode) {
		super(null, clusterNode, clazz.getName().toLowerCase(), clazz.getName());
		this.clazz = clazz;
	}

	public String creatMappings() throws Exception {
		String jsonData = ESDataUtil.getMappings(clazz);
		String url = super.getESHost(false);
		LOGGER.info(String.format("调用ES创建Mappings--参数：[%s] || 请求URL：%s", jsonData, url));
		return FeignUitls.httpPut(url, jsonData);
	}

	public String insert(T entity) throws Exception {
		ESData esData = ESDataUtil.getData(entity, clazz);
		String url = super.getESHost(true);
		LOGGER.info(String.format("调用ES插入单条记录--参数：[%s] || 请求URL：%s", esData.get_data(), url));
		return FeignUitls.httpPut(url + "/" + esData.get_id(), esData.get_data());
	}
	
	public String insertBatch(List<T> entitys) throws Exception {
		String entitysData = ESDataUtil.getDataByBatch(entitys, clazz);
		String clusterNode = super.getclusterNode();
		LOGGER.info(String.format("调用ES批量插入记录--参数：[%s] || 请求URL：%s", entitysData, clusterNode));
		return FeignUitls.httpPut(clusterNode + "/_bulk", entitysData);
	}

	public T get(Serializable id) throws Exception {
		String url = super.getESHost(true);
		String data = FeignUitls.httpGet(url + "/" + id);
		JSONObject jsonObject = JSON.parseObject(data);
		JSONObject soruce = jsonObject.getJSONObject("_source");
		String message = soruce.getString("message");
		T date = ESDataUtil.esDataToObj(message, clazz);
		return date;
	}

	public List<T> find(List<QueryParam> queryParams) throws Exception {
		String queryParam = ESDataUtil.getQueryParam(queryParams);
		String url = super.getESHost(true);
		LOGGER.info(String.format("调用ES查询记录--参数：[%s] || 请求URL：%s", queryParam, url));
		String data = FeignUitls.httpPost(url + "/_search", queryParam);
		return ESDataUtil.esDataToList(data, clazz);
	}

	public Pager<T> findByPage(List<QueryParam> queryParams, Pager<T> pager) throws Exception {
		String queryParam = ESDataUtil.getQueryParam(queryParams, pager.getStart(), pager.getPageSize());
		String url = super.getESHost(true);
		LOGGER.info(String.format("调用ES分页查询记录--参数：[%s] || 请求URL：%s", queryParam, url));
		String data = FeignUitls.httpPost(url + "/_search", queryParam);
		return ESDataUtil.esDataToPager(data, clazz, pager);
	}


}
